package com.wang.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wang.common.dao.impl.BaseDaoImpl;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.dao.IARoleMenuDao;
import com.wang.users.pojo.ARoleMenu;

/**
 * 角色菜单关系表
 * @author Zjx
 *
 */
public class ARoleMenuDaoImpl extends BaseDaoImpl<ARoleMenu> implements IARoleMenuDao
{

	@Override
	public int saveOne(ARoleMenu roleMenu)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("insert into a_role_menu (roleId, menuId, status, createTime, updateTime, pubTime) ");
		sb.append(" values ");
		sb.append(" (?,?,?,?,?,?) ");
		
		paramsList.add(roleMenu.getRoleId());
		paramsList.add(roleMenu.getMenuId());
		paramsList.add(roleMenu.getStatus());
		paramsList.add(roleMenu.getCreateTime());
		paramsList.add(roleMenu.getUpdateTime());
		paramsList.add(roleMenu.getPubTime());
		
		/**
		 * 执行sql语句
		 */
		int res =  executeSql(sb.toString(),paramsList);
		/* 处理主键 */
		int id = (int) paramsList.get(paramsList.size() - 1);
		roleMenu.setId(id);
		return res ; 
	}

	@Override
	public int updateOne(ARoleMenu roleMenu)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("update a_role_menu set ");
		sb.append(" roleId= ?, menuId= ?, status= ?, createTime= ?, updateTime= ?, pubTime = ? ");
		sb.append(" where id = ? ");
		
		paramsList.add(roleMenu.getRoleId());
		paramsList.add(roleMenu.getMenuId());
		paramsList.add(roleMenu.getStatus());
		paramsList.add(roleMenu.getCreateTime());
		paramsList.add(roleMenu.getUpdateTime());
		paramsList.add(roleMenu.getPubTime());
		paramsList.add(roleMenu.getId());
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public int deleteOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("delete from a_role_menu ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else if(condMap.get("roleId") != null && !"".equalsIgnoreCase(condMap.get("roleId") + ""))
		{
			sb.append(" where roleId = ? ");
			paramsList.add(condMap.get("roleId"));
		}else
		{
			/* 不传条件不能删除 */
			return 0 ; 
		}
		
		/* 执行sql语句 */
		return this.executeSql(sb.toString(), paramsList);
	}

	@Override
	public ARoleMenu findOne(Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_role_menu ");
		if(condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sb.append(" where id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			return null ; 
		}
		
		return findOneSql(sb.toString(), paramsList,ARoleMenu.class);
	}

	@Override
	public List<ARoleMenu> findCondList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个List;条件;就是每一个?对应的值 */
		List<Object> paramsList = new ArrayList<Object>();
		/* sql语句 */
		StringBuilder sb = new StringBuilder();
		sb.append("select * from a_role_menu where 1 = 1 ");
		
		/* 状态 */
		if(condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sb.append(" and status = ?  ");
			paramsList.add(condMap.get("status"));
		}
		
		/* 菜单Id */
		if(condMap.get("menuId") != null && !"".equalsIgnoreCase(condMap.get("menuId") + ""))
		{
			sb.append(" and menuId = ? ") ;
			paramsList.add(condMap.get("menuId"));
		}
		
		/* 角色Id */
		if(condMap.get("roleId") != null && !"".equalsIgnoreCase(condMap.get("roleId") + ""))
		{
			sb.append(" and roleId = ? ") ;
			paramsList.add(condMap.get("roleId"));
		}
		
		/* 时间 */
		if(condMap.get("st") instanceof Date && condMap.get("ed") instanceof Date )
		{
			sb.append(" and pubTime >= ? and pubTime < ? ");
			paramsList.add(condMap.get("st"));
			paramsList.add(condMap.get("ed"));
		}
		
		/* 排序 */
		if("idDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			sb.append(" order by id desc ") ; 
		}else
		{
			sb.append(" order by pubTime desc ");
		}
		
		if(pageInfoUtil != null)
		{
			return this.findListSql(pageInfoUtil, sb.toString(), paramsList,ARoleMenu.class);
		}
		return this.findListSql(sb.toString(), paramsList,ARoleMenu.class); 
	}

}
